package com.itheima.controller;

import com.itheima.Log.Log;
import com.itheima.common.PageBean;
import com.itheima.common.R;
import com.itheima.dto.ClazzAddDto;
import com.itheima.dto.ClazzQueryDto;
import com.itheima.pojo.Clazz;
import com.itheima.service.ClazzService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    ClazzService clazzService;

    @GetMapping
    public R query(ClazzQueryDto dto) {
        PageBean pageBean = clazzService.query(dto);
        return R.ok(pageBean);
    }

    @PostMapping
    @Log
    public R add(@RequestBody ClazzAddDto dto) {
        clazzService.add(dto);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @Log
    public R deleteid(@PathVariable Integer id) {
        clazzService.deleteid(id);
        return R.ok();
    }

    @GetMapping("/{id}")
    public R selectid(@PathVariable Integer id) {
        Clazz clazz = clazzService.selectid(id);
        return R.ok(clazz);
    }

    @PutMapping
    @Log
    public R update(@RequestBody Clazz clazz) {
        clazzService.update(clazz);
        return R.ok();
    }

    @GetMapping("/list")
    public R selectall() {
        List<Clazz> clazzes = clazzService.selectall();
        return R.ok(clazzes);
    }

}
